<?php

class Tools{
	
	/**
	* GET 请求
	* @param string $url
	*/
	public static function http_get($url){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}

	/*
	* POST 请求
	* @param string $url
	* @param array $param 
	* @return string content
	*/
	public static function http_post($url,$param){ 
		$oCurl = curl_init();
		$strPOST =  http_build_query($param);
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
		} 
		$cUrl = $url.'?'.$strPOST;
		Tools::_L($cUrl);
		//echo '<br />'.$cUrl.'<br />';
		curl_setopt($oCurl, CURLOPT_URL, $cUrl);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($oCurl, CURLOPT_POST,true);
		curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
		//curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		 
		//echo '<pre />';
		//print_r(json_decode($sContent,true));
		//echo '<pre />';
		 
		Tools::_L($sContent);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		} 
	}
	
	
	/*
	* POST 请求
	* @param string $url
	* @param array $param 
	* @return string content
	*/
	public static function http_post2($url,$sysparam,$param){ 
		$oCurl = curl_init();
		$strPOST =  json_encode($param);
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
		}  
		$queryStr =  http_build_query($sysparam);
		//$this->_L('S|'.$sysparam);
		$url .= '?'.$queryStr;
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($oCurl, CURLOPT_POST,true);
		curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
		curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		//$this->_L('R'.$sContent); 
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		} 
	}
	
	public static function _L($obj){
		$str = '';
		if(is_array($obj)){
			$arr = array();
			foreach($obj as $k => $v){
				$arr[] = $k.'='.$v;
			}
			$str = implode('|',$arr);
		}else{
			$str = $obj;
		}
		$str = '【'.date('H:i:s',time()).'】'.$str.'
';
		$filename = 'public/log.log';
		$file = fopen($filename,'a+');
		fwrite($file,$str);
		fclose($file);
	}
	
}
?>